package com.nd.mobile.study.view;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.common.util.ExceptionHandler;
import com.nd.mobile.study.common.util.L;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.common.util.VitamioInstallerUtil;
import com.nd.mobile.study.domain.Subject;
import com.nd.mobile.study.domain.User;
import com.nd.mobile.study.domain.VideoInfo;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;
import com.nd.mobile.study.view.helper.ToastHelper;

public class VideoCatalogActivity extends BaseActivity {
	private ListView			mLV;
	private VideoCatalogAdapter	mAdapter;
	private VideoInfo[]			mVideoInfos;
	private Subject				mSubject;
	private TextView			mTVListTitle;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.class_info);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.tv_app_video);
		mHeaderBtnHome.setVisibility(View.VISIBLE);

		mTVListTitle = (TextView) findViewById(R.id.tv_list_title);
		mTVListTitle.setText("选择视频");
		mLV = (ListView) findViewById(R.id.common_lv);
	}

	@Override
	protected void setFieldValues() {
		mSubject = (Subject) getIntent().getSerializableExtra(BundleConstants.SUBJECT_INFO);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getVideoInfo();
	}

	private void getVideoInfo() {
		mLoading.p();
		new GetVideoInfoTask(this).execute();
	}

	private class GetVideoInfoTask extends SimpleAsyncTask<Void, Void, VideoInfo[]> {

		public GetVideoInfoTask(Context context) {
			super(context);
		}

		@Override
		protected VideoInfo[] onLoad(Void... params) throws Exception {
			return mSubject.getVideoCatalog(App.getInstance().getUser());
		}

		@Override
		protected void onUI(VideoInfo[] result) {
			mVideoInfos = result;
			mAdapter = new VideoCatalogAdapter(mContext, mVideoInfos);
			mLV.setAdapter(mAdapter);
			mLoading.v();
		}

	}

	private class VideoCatalogAdapter extends SimpleListAdapter<VideoInfo> {

		public VideoCatalogAdapter(Context context, VideoInfo[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvName;
			TextView	tvDescr;
			Button		btnPlay;
		}

		private Intent		mIntent;

		private AlertDialog	mDlg;

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.video_list_item, null);

				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.video_list_tv_name);
				holder.tvDescr = (TextView) convertView.findViewById(R.id.video_list_tv_descr);
				holder.btnPlay = (Button) convertView.findViewById(R.id.video_list_btn_play);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final VideoInfo currData = mData.get(position);
			holder.tvName.setText(currData.getName());
			holder.tvDescr.setText(currData.getResDescription());
			holder.btnPlay.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					User user;
					try {
						user = App.getInstance().getUser();
					} catch (LocalErrorException e) {
						e.printStackTrace();
						String errorMsg = ExceptionHandler.parseExceptionInfo(e);
						ToastHelper.displayToast(getContext(), errorMsg);
						return;
					}
					
					SharedPreferencesUtil sp = SharedPreferencesUtil.getUserViedoInfoSp(getContext(), user);
					
					int elapsedTime = 0;
					try {
						elapsedTime = sp.getIntValue(currData.getId());
					} catch (Exception e) {
						elapsedTime = (int) sp.getLong(currData.getId());
					}
					mIntent = new Intent();
					mIntent.setClass(getContext(), VideoPlayerActivity.class).putExtra(BundleConstants.VIDEO_INFO, currData);
					if (0 < elapsedTime) {
						showDlg(elapsedTime);
					} else {
						L.v(this.getClass(), "from 0ms");
						prepareForPlay();
					}
				}
			});

			return convertView;
		}

		private void showDlg(int elapsedTime) {
			if (null == mDlg) {
				Builder builder = new AlertDialog.Builder(getContext());
				mDlg = builder.setTitle("友情提示").setMessage("是否继续播放").create();
			}
			
			DialogOnClickListener onClickListener = new DialogOnClickListener(elapsedTime);
			mDlg.setButton(Dialog.BUTTON_POSITIVE, "是,继续", onClickListener);
			mDlg.setButton(Dialog.BUTTON_NEGATIVE, "否,重头开始", onClickListener);
			
			mDlg.show();
		}

		private class DialogOnClickListener implements Dialog.OnClickListener {
			private int	elapsedTime;

			public DialogOnClickListener(int elapsedTime) {
				this.elapsedTime = elapsedTime;
			}

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case Dialog.BUTTON_POSITIVE:
					L.v(this.getClass(), "from last");
					L.v(this.getClass(), "elapsedTime = " + elapsedTime);
					mIntent.putExtra(BundleConstants.VIDEO_BEGIN_TIME, elapsedTime);
					prepareForPlay();
					break;
				case Dialog.BUTTON_NEGATIVE:
					L.v(this.getClass(), "from 0ms");
					prepareForPlay();
					break;
				default:
					break;
				}
			}
		}

		private void prepareForPlay() {
			if (VitamioInstallerUtil.check(getContext())) {
				startActivity(mIntent);
			}
		}
	}
}
